package com.jwaoo.chat.viewholder;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dx.as.common.log.LogUtil;
import com.dx.as.common.string.StringUtil;
import com.dx.as.upload.DownloadManager;
import com.dx.as.upload.OnDownloadListener;
import com.jwaoo.chat.ChatConstants;
import com.jwaoo.chat.R;
import com.jwaoo.chat.data.db.MessageHandler;
import com.jwaoo.chat.session.snap.WatchSnapChatPictureActivity;
import com.jwaoo.chat.session.video.WatchVideoActivity;
import com.jwaoo.chat.ui.recyclerview.adpter.BaseMultiItemFetchLoadAdapter;

/**
 * Created by zhoujianghua on 2015/8/7.
 */
public class MsgViewHolderSnapChat extends MsgViewHolderBase {

    private ImageView thumbnailImageView;

    protected View progressCover;
    private TextView progressLabel;

    public MsgViewHolderSnapChat(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.nim_message_item_snapchat;
    }

    @Override
    protected void inflateContentView() {
        thumbnailImageView = (ImageView) view.findViewById(R.id.message_item_snap_chat_image);
        progressBar = findViewById(R.id.message_item_thumb_progress_bar); // 覆盖掉
        progressCover = findViewById(R.id.message_item_thumb_progress_cover);
        progressLabel = (TextView) view.findViewById(R.id.message_item_thumb_progress_text);
    }

    @Override
    protected void bindContentView() {
        contentContainer.setOnTouchListener(onTouchListener);

        layoutByDirection();

        refreshStatus();
        downLoad();
    }

    private void refreshStatus() {
        thumbnailImageView.setBackgroundResource(isReceivedMessage() ? R.drawable.message_view_holder_left_snapchat : R.drawable.message_view_holder_right_snapchat);

        if (message.getState() == ChatConstants.sending || message.getAttachment().getAttachstate() == ChatConstants.transferring) {
            progressCover.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.VISIBLE);
        } else {
            progressCover.setVisibility(View.GONE);
        }
        progressLabel.setText(StringUtil.getPercentString(getMsgAdapter().getProgress(message)));
    }

    private void downLoad(){
        if (message.getAttachment().getAttachstate() == ChatConstants.transferred) return;
        if (!DownloadManager.isDownloading(message.getMsgid())){
            if (message.getDirect() == ChatConstants.Out){
                MessageHandler.getInstace(context).uploadAttachmentMessage(message);
            }else {
                MessageHandler.getInstace(context).handlerAttachmentMessage(message);
            }
        }
        DownloadManager.addDownloadListener(message.getMsgid(), new OnDownloadListener() {
            @Override
            public void onDownloadFailed() {
                message.getAttachment().setAttachstate(ChatConstants.fail);
            }

            @Override
            public void onDownSuccess() {
                message.getAttachment().setAttachstate(ChatConstants.transferred);
            }

            @Override
            public void onDownProgress(final long progress,final long total) {
                getMsgAdapter().getHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        progressLabel.setText(StringUtil.getPercentString(progress * 100/total));
                        LogUtil.d("SnapChat",StringUtil.getPercentString(progress * 100/total));
                    }
                });
            }
        });
    }

    protected View.OnTouchListener onTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    if (message.getMsgtype() == ChatConstants.video) {
                        WatchVideoActivity.destroy();
                    }else if (message.getMsgtype() == ChatConstants.image){
                        WatchSnapChatPictureActivity.destroy();
                    }
//                    // 删除这条消息，当然你也可以将其标记为已读，同时删除附件内容，然后不让再查看
//                    if (isLongClick && message.getAttachment().getAttachstate() == ChatConstants.transferred) {
//                        // 物理删除
//                        MessageHandler.getInstace(context).handlerMessages(MessageHandler.DELETE_MESSAGE,message);
//                        AttachmentStore.delete(message.getAttachment().getPath());
//                        AttachmentStore.delete(message.getAttachment().getThumbPath());
//                        getMsgAdapter().deleteItem(message, true);
//                        isLongClick = false;
//                    }
                    break;
            }

            return false;
        }
    };

    @Override
    protected boolean onItemLongClick() {
        super.onItemLongClick();
        if (message.getState() == ChatConstants.success
                && message.getAttachment().getAttachstate() == ChatConstants.transferred) {
            if (message.getMsgtype() == ChatConstants.image){
                WatchSnapChatPictureActivity.start(context, message);
            }else {
                WatchVideoActivity.start(context,message);
            }
            return true;
        }
        return false;
    }

    @Override
    protected int leftBackground() {
        return 0;
    }

    @Override
    protected int rightBackground() {
        return 0;
    }

    private void layoutByDirection() {
        View body = findViewById(R.id.message_item_snap_chat_body);
        View tipsLayout = findViewById(R.id.message_item_tips_layout);
        View tips = findViewById(R.id.message_item_snap_chat_tips_label);
        View readed = findViewById(R.id.message_item_snap_chat_readed);
        ViewGroup container = (ViewGroup) body.getParent();
        container.removeView(tipsLayout);
        if (isReceivedMessage()) {
            container.addView(tipsLayout, 1);
        } else {
            container.addView(tipsLayout, 0);
        }
        if (message.getState() == ChatConstants.success) {
            tips.setVisibility(View.VISIBLE);
        } else {
            tips.setVisibility(View.GONE);
        }

        //借助于已读回执
        if (getMsgAdapter().getUuid() != 0 && (message.getMsgid()== getMsgAdapter().getUuid()) ){
            readed.setVisibility(View.VISIBLE);
        } else {
            readed.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onItemClick() {
        super.onItemClick();
    }
}
